<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/22
 * Time: 下午5:54
 */

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\RoomMicroServices;
use App\Http\Services\RoomMsgServices;
use App\Http\Services\RoomServicesInit;
use App\Http\Services\SystemConfigServices;
use Illuminate\Http\Request;
use OSS\Core\OssUtil;
use App\Lib\Response;


class RoomMicroController extends Controller
{
    private $services_room_micro;
    private $services_room_msg;
    private $micro_jump_lock_key = "micro:jump:msg";
    public function __construct(RoomMsgServices $roomMsgServices)
    {
        $this->services_room_micro = RoomServicesInit::createMicro();
        $this->services_room_msg = new RoomMsgServices();
    }


    /**
     * 获得麦列表
     * @param Request $request
     */
    public function get_micros(Request $request){
        $room_id = $request->get("room_id");
        if ($room_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->services_room_micro->get_micros($room_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        Response::returnResult(Hint::SUCCESS, $data);
    }


    /**
     * 抱上麦
     * @param Request $request
     */
    public function into_req(Request $request){
        $room_id = $request->get("room_id");
        $micro_id = $request->get("microphone_id");
        $user_id = $request->get("user_id");
        if ($room_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($micro_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($user_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->services_room_micro->into_req($room_id, $micro_id, $user_id);
        //守护麦报错类型
        if($data === Hint::ROOM_MICRO_GUARD_NOT_EXIST){
            $protectName = [
                '0' => '黄金',
                '1' => '白银',
                '2' => '白银',
                '3' => '白银',
                '4' => '青铜',
                '5' => '青铜',
                '6' => '青铜',
                '7' => '青铜',
            ];
            Response::returnResult(Hint::ROOM_MICRO_GUARD_NOT_EXIST,[],'用户非'.$protectName[$micro_id].'守护，不满足上麦等级');
        }
        if($data === Hint::ROOM_MICRO_GUARD_MATCH){
            $protectName = [
                '0' => '黄金',
                '1' => '白银',
                '2' => '白银',
                '3' => '白银',
                '4' => '青铜',
                '5' => '青铜',
                '6' => '青铜',
                '7' => '青铜',
            ];
            Response::returnResult(Hint::ROOM_MICRO_GUARD_MATCH,[],'用户非'.$protectName[$micro_id].'守护，不满足上麦等级');
        }
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
        }
        if ($data === Hint::ROOM_MICRO_IS_CP){
            Response::returnResult(Hint::ROOM_MICRO_IS_CP);
        }
        if ($data === Hint::NOT_ALLOWED){
            Response::returnResult(Hint::NOT_ALLOWED);
        }
        if ($data === Hint::ROOM_MICRO_GUARD_MATCH){
            Response::returnResult(Hint::ROOM_MICRO_GUARD_MATCH);
        }
        if ($data === Hint::ROOM_HOSTMICRO_IS_NULL){
            Response::returnResult(Hint::ROOM_HOSTMICRO_IS_NULL);
        }
        if ($data === Hint::ROOM_MICRO_NOT_NULL){
            Response::returnResult(Hint::ROOM_MICRO_NOT_NULL);
        }
        if ($data === Hint::ROOM_MICRO_IS_LOCK){
            Response::returnResult(Hint::ROOM_MICRO_IS_LOCK);
        }
        if ($data === Hint::ROOM_MICRO_USER_IS_EXIST){
            Response::returnResult(Hint::ROOM_MICRO_USER_IS_EXIST);
        }
        if ($data === Hint::ROOM_USER_NOT_EXIST){
            Response::returnResult(Hint::ROOM_USER_NOT_EXIST);
        }
        $this->services_room_msg->command_micro_into($room_id, $micro_id, $user_id, 1);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 麦上锁
     * @param Request $request
     */
    public function lock(Request $request){
        $room_id = $request->get("room_id");
        $micro_id = $request->get("microphone_id");
        if ($room_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($micro_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->services_room_micro->lock($room_id, $micro_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
        }
        if ($data === Hint::ROOM_MICRO_USER_IS_EXIST){
            Response::returnResult(Hint::ROOM_MICRO_USER_IS_EXIST);
        }
        $this->services_room_msg->broadcast_micro_lock($room_id, $micro_id);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 麦解锁
     * @param Request $request
     */
    public function unlock(Request $request){
        $room_id = $request->get("room_id");
        $micro_id = $request->get("microphone_id");
        if ($room_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($micro_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->services_room_micro->unlock($room_id, $micro_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
        }
        if ($data === Hint::ROOM_MICRO_USER_IS_EXIST){
            Response::returnResult(Hint::ROOM_MICRO_USER_IS_EXIST);
        }
        $this->services_room_msg->broadcast_micro_unlock($room_id, $micro_id);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 设置老板位
     * @param Request $request
     */
    public function setboss(Request $request){
        $room_id = $request->get("room_id");
        $micro_id = $request->get("microphone_id");
        if ($room_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($micro_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->services_room_micro->setboss($room_id, $micro_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
        }
        $this->services_room_msg->broadcast_micro_setboss($room_id, $micro_id);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 取消老板位设置
     * @param Request $request
     */
    public function unsetboss(Request $request){
        $room_id = $request->get("room_id");
        $micro_id = $request->get("microphone_id");
        if ($room_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($micro_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->services_room_micro->unsetboss($room_id, $micro_id);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
        }
        $this->services_room_msg->broadcast_micro_unsetboss($room_id, $micro_id);
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 打开麦克风
     * @param Request $request
     */
    public function open(Request $request){
        //增加限制房间开启
        $roomAndVolumeCloseConfig = (new SystemConfigServices())->getRoomAndVolumeCloseConfig();
        $currentTime = time();
        if ($roomAndVolumeCloseConfig[2] > 0 && $roomAndVolumeCloseConfig[3] > 0
            && $currentTime > $roomAndVolumeCloseConfig[2] && $currentTime < $roomAndVolumeCloseConfig[3]){
                Response::returnResult(Hint::ERR_SYS_FAIL, [], '维护中，请耐心等待');
        }

        $room_id = $request->get("room_id");
        $micro_id = $request->get("microphone_id");
        $self = $request->get("self");
        if ($room_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($micro_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($self == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $boolSelf = true;
        if ($self == 0){
            $boolSelf = false;
        }
        $data = $this->services_room_micro->open($room_id, $micro_id, $boolSelf);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
        }
        if ($data === Hint::USER_MICRO_IS_BLOCK){
            Response::returnResult(Hint::USER_MICRO_IS_BLOCK);
            return;
        }
        if($data === Hint::ROOM_MIC_FORBID){
            Response::returnResult(Hint::ROOM_MIC_FORBID,[],'房间处于禁麦状态,无法进行此操作');
        }
        $this->services_room_msg->command_micro_volume_open($room_id, $micro_id, $self);
        $this->services_room_msg->broadcast_micro_volume_open($room_id, $micro_id, $self);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 关闭麦克风
     * @param Request $request
     */
    public function close(Request $request){
        $room_id = $request->get("room_id");
        $micro_id = $request->get("microphone_id");
        $self = $request->get("self");
        if ($room_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($micro_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($self == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $boolSelf = true;
        if ($self == 0){
            $boolSelf = false;
        }
        $data = $this->services_room_micro->close($room_id, $micro_id, $boolSelf);
        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data === Hint::AUTH_ERROR){
            Response::returnResult(Hint::AUTH_ERROR);
        }
        $this->services_room_msg->command_micro_volume_close($room_id, $micro_id, $self);
        $this->services_room_msg->broadcast_micro_volume_close($room_id, $micro_id, $self);
        Response::returnResult(Hint::SUCCESS);
    }



    /**
     * 上麦(抱上麦后续操作)
     * @param Request $request
     */
    public function into(Request $request){
        $room_id = $request->get("room_id");
        $micro_id = $request->get("microphone_id");
        if ($room_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($micro_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $user_id = null;
        $data = $this->services_room_micro->into($room_id, $micro_id, $user_id);
        //守护麦报错类型
        if($data === Hint::ROOM_MICRO_GUARD_NOT_EXIST){
            $protectName = [
                '0' => '黄金',
                '1' => '白银',
                '2' => '白银',
                '3' => '白银',
                '4' => '青铜',
                '5' => '青铜',
                '6' => '青铜',
                '7' => '青铜',
            ];
            Response::returnResult(Hint::ROOM_MICRO_GUARD_NOT_EXIST,[],'非'.$protectName[$micro_id].'守护，不满足上麦等级');
        }
        if($data === Hint::ROOM_MICRO_GUARD_MATCH){
            $protectName = [
                '0' => '黄金',
                '1' => '白银',
                '2' => '白银',
                '3' => '白银',
                '4' => '青铜',
                '5' => '青铜',
                '6' => '青铜',
                '7' => '青铜',
            ];
            Response::returnResult(Hint::ROOM_MICRO_GUARD_MATCH,[],'非'.$protectName[$micro_id].'守护，不满足上麦等级');
        }

        if ($data === Hint::LOGIN_OUT){
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data === Hint::NOT_ALLOWED){
            Response::returnResult(Hint::NOT_ALLOWED);
        }
        if ($data === Hint::ROOM_MICRO_IS_CP){
            Response::returnResult(Hint::ROOM_MICRO_IS_CP);
        }
        if ($data === Hint::ROOM_MICRO_GUARD_MATCH){
            Response::returnResult(Hint::ROOM_MICRO_GUARD_MATCH);
        }
        if ($data === Hint::ROOM_HOSTMICRO_IS_NULL){
            Response::returnResult(Hint::ROOM_HOSTMICRO_IS_NULL);
        }
        if ($data === Hint::ROOM_MICRO_NOT_NULL){
            Response::returnResult(Hint::ROOM_MICRO_NOT_NULL);
        }
        if ($data === Hint::ROOM_MICRO_IS_LOCK){
            Response::returnResult(Hint::ROOM_MICRO_IS_LOCK);
        }
        if ($data === Hint::ROOM_MICRO_USER_IS_EXIST){
            Response::returnResult(Hint::ROOM_MICRO_USER_IS_EXIST);
        }
        if ($data === Hint::ROOM_USER_NOT_EXIST){
            Response::returnResult(Hint::ROOM_USER_NOT_EXIST);
        }
        if ($data === Hint::ROOM_MICRO_USER_LOCK){
            Response::returnResult(Hint::ROOM_MICRO_USER_LOCK, [], '您已被锁麦，不可操作');
        }

        $this->services_room_msg->command_micro_into($room_id, $micro_id, $user_id);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * 用户跳麦
     * @param Request $request
     */
    public function jump(Request $request){
        $room_id = $request->get("room_id");
        $jump_micro_id = $request->get("microphone_id");
        if ($room_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if ($jump_micro_id == null){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if (!redis_lock($this->micro_jump_lock_key .$room_id, 10)){
            Response::returnResult(Hint::NOT_ALLOWED);
        }
        $user_id = null;
        $micro_id = null;
        $volume_self = true;
        $volume = true;
        $data = $this->services_room_micro->jump($room_id, $jump_micro_id, $micro_id, $user_id, $volume, $volume_self);
        //守护麦报错类型
        if($data === Hint::ROOM_MICRO_GUARD_NOT_EXIST){
            Response::returnResult(Hint::ROOM_MICRO_GUARD_NOT_EXIST);
        }
        if($data === Hint::ROOM_MICRO_GUARD_MATCH){
            $protectName = [
                '0' => '黄金',
                '1' => '白银',
                '2' => '白银',
                '3' => '白银',
                '4' => '青铜',
                '5' => '青铜',
                '6' => '青铜',
                '7' => '青铜',
            ];
            Response::returnResult(Hint::ROOM_MICRO_GUARD_MATCH,[],'非'.$protectName[$jump_micro_id].'守护，不满足上麦等级');
        }
        if ($data === Hint::LOGIN_OUT){
            redis_unlock($this->micro_jump_lock_key .$room_id);
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if ($data === Hint::NOT_ALLOWED){
            redis_unlock($this->micro_jump_lock_key .$room_id);
            Response::returnResult(Hint::NOT_ALLOWED);
        }

        if ($data === Hint::ROOM_MICRO_IS_CP){
            redis_unlock($this->micro_jump_lock_key .$room_id);
            Response::returnResult(Hint::ROOM_MICRO_IS_CP);
        }
        if ($data === Hint::ROOM_MICRO_GUARD_MATCH){
            redis_unlock($this->micro_jump_lock_key .$room_id);
            Response::returnResult(Hint::ROOM_MICRO_GUARD_MATCH);
        }
        if ($data === Hint::ROOM_HOSTMICRO_IS_NULL){
            redis_unlock($this->micro_jump_lock_key .$room_id);
            Response::returnResult(Hint::ROOM_HOSTMICRO_IS_NULL);
        }
        if ($data === Hint::ROOM_MICRO_NOT_NULL){
            redis_unlock($this->micro_jump_lock_key .$room_id);
            Response::returnResult(Hint::ROOM_MICRO_NOT_NULL);
        }
        if ($data === Hint::ROOM_MICRO_IS_LOCK){
            redis_unlock($this->micro_jump_lock_key .$room_id);
            Response::returnResult(Hint::ROOM_MICRO_IS_LOCK);
        }
        if ($data === Hint::ROOM_MICRO_USER_NOT_EXIST){
            redis_unlock($this->micro_jump_lock_key .$room_id);
            Response::returnResult(Hint::ROOM_MICRO_USER_NOT_EXIST);
        }
        if ($data === Hint::ROOM_MICRO_USER_LOCK){
            redis_unlock($this->micro_jump_lock_key .$room_id);
            Response::returnResult(Hint::ROOM_MICRO_USER_LOCK, [], '您已被锁麦，不可操作');
        }
        if ($data === Hint::ROOM_PAIPAI_LOCK){
            redis_unlock($this->micro_jump_lock_key .$room_id);
            Response::returnResult(Hint::ROOM_PAIPAI_LOCK, [], '已产生心动值不可跳麦');
        }

        $this->services_room_msg->broadcast_micro_out($room_id, $micro_id, $user_id);
        $this->services_room_msg->broadcast_micro_into($room_id, $jump_micro_id, $user_id);
        $this->services_room_msg->broadcast_micro_ungift($room_id, $micro_id);
        if (!$volume_self){
            $this->services_room_msg->broadcast_micro_volume_close($room_id, $jump_micro_id, 1);
        }
        if (!$volume){
            $this->services_room_msg->broadcast_micro_volume_close($room_id, $jump_micro_id, 0);
        }
        redis_unlock($this->micro_jump_lock_key .$room_id);
        Response::returnResult(Hint::SUCCESS);
    }


    /**
     * h5 打开麦克风
     * @param Request $request
     */
    public function h5_open(Request $request)
    {
        $room_id = $request->get("room_id");
        $user_id = $request->get("user_id");
        $self = $request->get("self");
        write_log('roomMic','h5_open','user_id='.$user_id.';room_id='.$room_id);
        if ($room_id == null) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }
        if ($user_id == null) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }
        if ($self == null) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }
        $data = $this->services_room_micro->h5_open($room_id, $user_id);
        $mic_id = $data['mic_id'];
        $mic_ids = $data['data'];
        if($mic_id !== null){
            //普通麦全部开启
            if($mic_id == -2){
                foreach ($mic_ids as $mid){
                    $this->services_room_msg->command_micro_volume_open($room_id, $mid, $self);
                    $this->services_room_msg->broadcast_micro_volume_open($room_id, $mid, $self);
                }
            }else{
                foreach ($mic_ids as $mid){
                    $this->services_room_msg->command_micro_volume_close($room_id, $mid, $self);
                    $this->services_room_msg->broadcast_micro_volume_close($room_id, $mid, $self);
                }
                if($mic_id >=0){
                    $this->services_room_msg->command_micro_volume_open($room_id, $mic_id, $self);
                    $this->services_room_msg->broadcast_micro_volume_open($room_id, $mic_id, $self);
                }
            }
            OutJson::outJsonResult(Hint::SUCCESS,$data);
        }
        OutJson::outJsonResult(Hint::ERROR);
    }

    /**
     * h5 关闭麦克风
     * @param Request $request
     */
    public function h5_close(Request $request){
        $room_id = $request->get("room_id");
        $user_id = $request->get("user_id");
        $self = $request->get("self");
        write_log('roomMic','h5_close','user_id='.$user_id.';room_id='.$room_id);
        if ($room_id == null){
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }
        if ($user_id == null){
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }
        if ($self == null){
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }
        $data = $this->services_room_micro->h5_close($room_id, $user_id);
        $mic_id = $data['mic_id'];
        $mic_ids = $data['data'];
        if($mic_id == -1){
            foreach ($mic_ids as $mid){
                $this->services_room_msg->command_micro_volume_close($room_id, $mid, $self);
                $this->services_room_msg->broadcast_micro_volume_close($room_id, $mid, $self);
            }
        }else{
            foreach ($mic_ids as $mid){
                $this->services_room_msg->command_micro_volume_open($room_id, $mid, $self);
                $this->services_room_msg->broadcast_micro_volume_open($room_id, $mid, $self);
            }
            if($mic_id>=0){
                $this->services_room_msg->command_micro_volume_close($room_id, $mid, $self);
                $this->services_room_msg->broadcast_micro_volume_close($room_id, $mid, $self);
            }
        }
        OutJson::outJsonResult(Hint::SUCCESS);
    }
}